package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.UserDao;
import com.gusha.model.SysArea;
import com.gusha.model.User;


/**
 * @Created：2018-11-06 14:11:32
 * @Author LightSound的代码生成器
 * @Description:UserService服务类
 */

@Service
@Transactional
public class UserService {

    @Autowired
    private UserDao userDao;

    public Page<User> findAll(Pageable pageable, User user) {
        @SuppressWarnings("serial")
        Page<User> result = userDao.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (user.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),user.getId()));
                }
                if (user.getUserType() != null) {
                    list.add(cb.equal(root.get("userType").as(Integer.class),user.getUserType()));
                }
                if (user.getBelongId() != null) {
                    list.add(cb.equal(root.get("belongId").as(BigInteger.class),user.getBelongId()));
                }
                if (StringUtils.isNotBlank(user.getUserName())) {
                    list.add(cb.equal(root.get("userName").as(String.class),user.getUserName()));
                }
                if (StringUtils.isNotBlank(user.getPassword())) {
                    list.add(cb.equal(root.get("password").as(String.class),user.getPassword()));
                }
                if (StringUtils.isNotBlank(user.getUserNo())) {
                    list.add(cb.equal(root.get("userNo").as(String.class),user.getUserNo()));
                }
                if (StringUtils.isNotBlank(user.getRealName())) {
                    list.add(cb.equal(root.get("realName").as(String.class),user.getRealName()));
                }
                if (StringUtils.isNotBlank(user.getCardNo())) {
                    list.add(cb.equal(root.get("cardNo").as(String.class),user.getCardNo()));
                }
                if (StringUtils.isNotBlank(user.getPhone())) {
                    list.add(cb.equal(root.get("phone").as(String.class),user.getPhone()));
                }
                if (StringUtils.isNotBlank(user.getAddress())) {
                    list.add(cb.equal(root.get("address").as(String.class),user.getAddress()));
                }
                if (user.getAreaProvince()!=null) {
                    list.add(cb.equal(root.get("areaProvince").as(SysArea.class),user.getAreaProvince()));
                }
                if (user.getAreaCity()!=null) {
                    list.add(cb.equal(root.get("areaCity").as(SysArea.class),user.getAreaCity()));
                }
                if (user.getAreaDistrict()!=null) {
                    list.add(cb.equal(root.get("areaDistrict").as(SysArea.class),user.getAreaDistrict()));
                }
                if (StringUtils.isNotBlank(user.getAreaSub())) {
                    list.add(cb.equal(root.get("areaSub").as(String.class),user.getAreaSub()));
                }
                if (user.getUserLevel() != null) {
                    list.add(cb.equal(root.get("userLevel").as(Long.class),user.getUserLevel()));
                }
                if (StringUtils.isNotBlank(user.getLon())) {
                    list.add(cb.equal(root.get("lon").as(String.class),user.getLon()));
                }
                if (StringUtils.isNotBlank(user.getLat())) {
                    list.add(cb.equal(root.get("lat").as(String.class),user.getLat()));
                }
                if (StringUtils.isNotBlank(user.getSex())) {
                    list.add(cb.equal(root.get("sex").as(String.class),user.getSex()));
                }
                if (user.getAge() != null) {
                    list.add(cb.equal(root.get("age").as(Integer.class),user.getAge()));
                }
                if (StringUtils.isNotBlank(user.getRegNo())) {
                    list.add(cb.equal(root.get("regNo").as(String.class),user.getRegNo()));
                }
                if (StringUtils.isNotBlank(user.getRegMain())) {
                    list.add(cb.equal(root.get("regMain").as(String.class),user.getRegMain()));
                }
                if (StringUtils.isNotBlank(user.getRegIcon())) {
                    list.add(cb.equal(root.get("regIcon").as(String.class),user.getRegIcon()));
                }
                if (StringUtils.isNotBlank(user.getCardIcon())) {
                    list.add(cb.equal(root.get("cardIcon").as(String.class),user.getCardIcon()));
                }
                if (user.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),user.getCreateTime()));
                }
                if (user.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),user.getUpdateTime()));
                }
                if (StringUtils.isNotBlank(user.getCreator())) {
                    list.add(cb.equal(root.get("creator").as(String.class),user.getCreator()));
                }
                if (StringUtils.isNotBlank(user.getUpdator())) {
                    list.add(cb.equal(root.get("updator").as(String.class),user.getUpdator()));
                }
                if (user.getTestMoney() != null) {
                    list.add(cb.equal(root.get("testMoney").as(BigInteger.class),user.getTestMoney()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
